package crmdna.list;

import crmdna.client.Client;
import crmdna.group.Group;
import crmdna.user.User;
import crmdna.user.User.Action;
import crmdna.user.User.ResourceType;

public class List {

	static ListProp create(String client, long groupId, String displayName,
			boolean restricted, boolean enabled, String login) {

		Client.ensureValid(client);

		Group.safeGet(client, groupId);

		User.ensureAccess(client, login, ResourceType.LIST, groupId,
				Action.WRITE);

		return ListCore.create(client, groupId, displayName, restricted,
				enabled);
	}

	static ListProp update(String client, long listId,
			String newDisplayName, Long newGroupId, Boolean newRestricted,
			Boolean newEnabled, String login) {

		Client.ensureValid(client);

		if (newGroupId != null) {
			Group.safeGet(client, newGroupId);
	
			ListEntity entity = ListCore.safeGet(client, listId);
			User.ensureAccess(client, login, ResourceType.LIST, newGroupId,
					Action.WRITE);
			User.ensureAccess(client, login, ResourceType.LIST, entity.groupId,
					Action.WRITE);			
		}

		return ListCore.update(client, listId, newDisplayName, newGroupId, newRestricted, newEnabled);
	}
}
